package examples;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PushbackReader;
import java.io.StringReader;
import java.util.Iterator;
import java.util.LinkedList;

import org.netbeans.api.mdr.MDRManager;
import org.netbeans.api.mdr.MDRepository;

import parser.astgen.Heritage;
import parser.astgen.LAttrGMCAstGenerator;
import parser.sablecc.lexer.Lexer;
import parser.sablecc.node.Start;
import parser.sablecc.parser.Parser;
import parser.sablecc.parser.ParserException;
import project.Project;
import converterOCL.ConverterOCL;

/**
 * DemoOcl20Parser.java
 * @author Antonio Villegas
 *
 * Created on 19 de julio de 2007, 15:27
 */


public class DemoOcl20Parser extends javax.swing.JFrame
{
 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Project p;
	boolean isprojectopen = false;
	long start_time, stop_time;
	MDRepository repository;
	
  /** Creates new form DemoOcl20Parser */
  public DemoOcl20Parser()
  {
	  // Delete old repository files  
	  (new File("mdr.btb")).delete();
      (new File("mdr.btd")).delete();
      (new File("mdr.btx")).delete();
      
      initComponents();
  }
  
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc=" C�digo Generado  ">//GEN-BEGIN:initComponents
  private void initComponents()
  {
    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jPanel2 = new javax.swing.JPanel();
    ProjectName = new javax.swing.JTextField();
    jLabel5 = new javax.swing.JLabel();
    CreateProjectButton = new javax.swing.JButton();
    jPanel3 = new javax.swing.JPanel();
    jLabel6 = new javax.swing.JLabel();
    SaveToXMIFilenameField = new javax.swing.JTextField();
    SaveToXMIButton = new javax.swing.JButton();
    jPanel4 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    ConstraintsArea = new javax.swing.JTextArea();
    ParseButton = new javax.swing.JButton();
    EraseOCLAreaButton = new javax.swing.JButton();
    LoadOCLFileButton = new javax.swing.JButton();
    OCLFilenameField = new javax.swing.JTextField();
    jPanel5 = new javax.swing.JPanel();
    jLabel7 = new javax.swing.JLabel();
    ImportXMIFilenameField = new javax.swing.JTextField();
    LoadXMIFileButton = new javax.swing.JButton();
    jPanel6 = new javax.swing.JPanel();
    jScrollPane2 = new javax.swing.JScrollPane();
    FeedBackArea = new javax.swing.JTextArea();

    //Disabling some GUI components at the beginning
    SaveToXMIFilenameField.setEnabled(false);
    SaveToXMIButton.setEnabled(false);
    ConstraintsArea.setEnabled(false);
    ParseButton.setEnabled(false);
    EraseOCLAreaButton.setEnabled(false);
    ImportXMIFilenameField.setEnabled(false);
    LoadXMIFileButton.setEnabled(false);
    OCLFilenameField.setEnabled(false);
    LoadOCLFileButton.setEnabled(false);
    
    
    this.addWindowListener(new WindowAdapter() {
    	public void windowClosing(WindowEvent e) {
    		(new File("mdr.btb")).delete();
            (new File("mdr.btd")).delete();
            (new File("mdr.btx")).delete();
    		System.exit(0);
    	}
    });
    //setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setResizable(false);
    jPanel1.setBackground(new java.awt.Color(255, 255, 255));
    jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
    jLabel1.setFont(new java.awt.Font("Arial", 0, 18));
    jLabel1.setText("Demo");

    jLabel2.setFont(new java.awt.Font("Arial", 1, 18));
    jLabel2.setForeground(new java.awt.Color(51, 102, 255));
    jLabel2.setText("Parser OCL 2.0 Eina GMC");

    jLabel3.setFont(new java.awt.Font("Arial", 0, 14));
    jLabel3.setForeground(new java.awt.Color(153, 153, 153));
    jLabel3.setText("Grup de Recerca en Modelitzaci\u00f3 Conceptual");

    jLabel4.setFont(new java.awt.Font("Arial", 0, 14));
    jLabel4.setForeground(new java.awt.Color(153, 153, 153));
    jLabel4.setText("Universitat Polit\u00e8cnica de Catalunya (UPC)");

    org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(jPanel1Layout.createSequentialGroup()
            .add(jLabel1)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(jLabel2))
          .add(jPanel1Layout.createSequentialGroup()
            .add(jLabel3)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 161, Short.MAX_VALUE)
            .add(jLabel4)))
        .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jPanel1Layout.createSequentialGroup()
        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(jLabel2)
          .add(jLabel1))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(jLabel3)
          .add(jLabel4))
        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Create a new project"));

    jLabel5.setFont(new java.awt.Font("Arial", 0, 12));
    jLabel5.setText("Insert a project's name:");

    CreateProjectButton.setText("Create");
    CreateProjectButton.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        CreateProjectButtonActionPerformed(evt);
      }
    });

    org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jPanel2Layout.createSequentialGroup()
        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
          .add(CreateProjectButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
          .add(org.jdesktop.layout.GroupLayout.TRAILING, ProjectName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE))
        .addContainerGap())
    );

    jPanel2Layout.linkSize(new java.awt.Component[] {CreateProjectButton, ProjectName, jLabel5}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

    jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jPanel2Layout.createSequentialGroup()
        .add(jLabel5)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(ProjectName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(CreateProjectButton)
        .addContainerGap())
    );

    jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Save Model to XMI"));
    jLabel6.setFont(new java.awt.Font("Arial", 0, 12));
    jLabel6.setText("Insert XMI filename:");

    SaveToXMIButton.setText("Save");
    SaveToXMIButton.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        SaveToXMIButtonActionPerformed(evt);
      }
    });

    org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
    jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(
      jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jLabel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
      .add(jPanel3Layout.createSequentialGroup()
        .add(SaveToXMIButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
        .addContainerGap())
      .add(jPanel3Layout.createSequentialGroup()
        .add(SaveToXMIFilenameField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
        .addContainerGap())
    );

    jPanel3Layout.linkSize(new java.awt.Component[] {SaveToXMIButton, SaveToXMIFilenameField, jLabel6}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

    jPanel3Layout.setVerticalGroup(
      jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jPanel3Layout.createSequentialGroup()
        .add(jLabel6)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(SaveToXMIFilenameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(SaveToXMIButton))
    );

    jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Constraints OCL 2.0"));
    ConstraintsArea.setColumns(20);
    ConstraintsArea.setRows(5);
    jScrollPane1.setViewportView(ConstraintsArea);

    ParseButton.setText("Parse");
    ParseButton.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        ParseButtonActionPerformed(evt);
      }
    });

    EraseOCLAreaButton.setText("Erase");
    EraseOCLAreaButton.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        EraseOCLAreaButtonActionPerformed(evt);
      }
    });

    LoadOCLFileButton.setText("Load OCL File");
    LoadOCLFileButton.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        LoadOCLFileButtonActionPerformed(evt);
      }
    });


    org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
    jPanel4.setLayout(jPanel4Layout);
    jPanel4Layout.setHorizontalGroup(
      jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jPanel4Layout.createSequentialGroup()
        .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
          .add(jPanel4Layout.createSequentialGroup()
            .add(LoadOCLFileButton)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(OCLFilenameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 132, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(EraseOCLAreaButton)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(ParseButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 101, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
          .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel4Layout.createSequentialGroup()
            .add(20, 20, 20)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)))
        .addContainerGap())
    );

    jPanel4Layout.linkSize(new java.awt.Component[] {LoadOCLFileButton, EraseOCLAreaButton, ParseButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

    jPanel4Layout.setVerticalGroup(
      jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel4Layout.createSequentialGroup()
        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(EraseOCLAreaButton)
          .add(ParseButton)
          .add(LoadOCLFileButton)
          .add(OCLFilenameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addContainerGap())
    );

    jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Load XMI Model"));
    jLabel7.setFont(new java.awt.Font("Arial", 0, 12));
    jLabel7.setText("Insert XMI filename:");

    LoadXMIFileButton.setText("Load");
    LoadXMIFileButton.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        LoadXMIFileButtonActionPerformed(evt);
      }
    });

    org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
    jPanel5.setLayout(jPanel5Layout);
    jPanel5Layout.setHorizontalGroup(
      jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jPanel5Layout.createSequentialGroup()
        .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(jLabel7, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
          .add(ImportXMIFilenameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 171, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
          .add(LoadXMIFileButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
        .addContainerGap())
    );

    jPanel5Layout.linkSize(new java.awt.Component[] {LoadXMIFileButton, ImportXMIFilenameField, jLabel7}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

    jPanel5Layout.setVerticalGroup(
      jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jPanel5Layout.createSequentialGroup()
        .add(jLabel7)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(ImportXMIFilenameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .add(LoadXMIFileButton))
    );

    jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Parser Feedback"));
    FeedBackArea.setColumns(20);
    FeedBackArea.setEditable(false);
    FeedBackArea.setRows(5);
    FeedBackArea.setName("");
    jScrollPane2.setViewportView(FeedBackArea);

    org.jdesktop.layout.GroupLayout jPanel6Layout = new org.jdesktop.layout.GroupLayout(jPanel6);
    jPanel6.setLayout(jPanel6Layout);
    jPanel6Layout.setHorizontalGroup(
      jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 751, Short.MAX_VALUE)
    );
    jPanel6Layout.setVerticalGroup(
      jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    );

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .addContainerGap()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
          .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
              .add(jPanel2, 0, 219, Short.MAX_VALUE)
              .add(jPanel5, 0, 219, Short.MAX_VALUE)
              .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .addContainerGap()
        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(layout.createSequentialGroup()
            .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 116, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(jPanel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
          .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(jPanel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addContainerGap())
    );
    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void CreateProjectButtonActionPerformed(java.awt.event.ActionEvent evt)                                             
  {
	  // Code for the button named "CreateProject"
	  try {
		  if (isprojectopen) { //we must close it
			  //Disabling some components at the beginning
			  SaveToXMIFilenameField.setEnabled(false);
			  SaveToXMIButton.setEnabled(false);
			  ConstraintsArea.setEnabled(false);
			  ParseButton.setEnabled(false);
			  EraseOCLAreaButton.setEnabled(false);
			  ImportXMIFilenameField.setEnabled(false);
			  LoadXMIFileButton.setEnabled(false);
			  OCLFilenameField.setEnabled(false);
			  LoadOCLFileButton.setEnabled(false);
			  
			  p.closeProject();
			  isprojectopen = false;
		  }
		  //
		  String nomproject = ProjectName.getText();
		  if (!nomproject.equals("")) {
			  p = new Project(nomproject);
			  
			  //ConversorToEinaGMC ccc = new ConversorToEinaGMC();
			  //ccc.convert("/Users/lotendil/Desktop/models/ZZZempresa.xmi", "/Users/lotendil/Desktop/models/EEEempresa.xmi", "Poseidon");
			  
			  isprojectopen = true;
			  FeedBackArea.setText("Project "+nomproject+" was created successfully\n");
			  // All is ok, then enable the XMI-load components
			  ImportXMIFilenameField.setEnabled(true);
			  LoadXMIFileButton.setEnabled(true);
			  // and disable the create project components
			  ProjectName.setEnabled(false);
			  CreateProjectButton.setEnabled(false);
		  }
		  else { // the name must be inserted
			  FeedBackArea.setText("Firstly, insert the project's name.");
		  }
	  } catch (Exception e) {
		  FeedBackArea.setText("Error with the project's creation: "+e.getMessage());
		  FeedBackArea.append("\nTry it again with another name.");
	  }
  }

  private void LoadXMIFileButtonActionPerformed(java.awt.event.ActionEvent evt)                                              
  {
	  // Code for the button named  "LoadXMIFileButton"
	  start_time = System.currentTimeMillis();
	  String nomarxiu = ImportXMIFilenameField.getText();
	  boolean esbuit = nomarxiu.equals("");
	  if (!esbuit) {
		  try {	
			  FeedBackArea.setText("Loading from file "+nomarxiu+" ...");
			  // Disable the XMI-load components
			  ImportXMIFilenameField.setEnabled(false);
			  LoadXMIFileButton.setEnabled(false);
			  // Load the XMI file
			  p.importXMI("models/"+nomarxiu);
		  } catch (Exception e) {
			  FeedBackArea.setText("Error while loading: "+e.getMessage());
			  FeedBackArea.append("\nTry it again with another filename.");
			  try {
				p = new Project("");
			  } catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			  }
			  esbuit = true;
			  // Enable the XMI-load components
			  ImportXMIFilenameField.setEnabled(true);
			  LoadXMIFileButton.setEnabled(true);
		  }
		  
		  stop_time = System.currentTimeMillis();
		  if (!esbuit) {
			  FeedBackArea.append("OK!\n");
			  FeedBackArea.append("Load time: " + (stop_time - start_time) + " ms\n");
			  
			  // Test of ConversorOCL
			  ConverterOCL cocl = new ConverterOCL(p);
			  LinkedList con = cocl.convertToOcl();
			  Iterator it = con.iterator();
			  if (!con.isEmpty())
				  ConstraintsArea.setText("--Constraints loaded automatically from "+nomarxiu+"\n\n");
			  
			  while (it.hasNext()) {
				  ConstraintsArea.append((String) it.next());
				  ConstraintsArea.append("\n");
			  }
			  
			  // Enable the ConstraintsArea (with its buttons) and the Save-model components
			  SaveToXMIFilenameField.setEnabled(true);
			  SaveToXMIButton.setEnabled(true);
			  ConstraintsArea.setEnabled(true);
			  ParseButton.setEnabled(true);
			  EraseOCLAreaButton.setEnabled(true);
			  LoadOCLFileButton.setEnabled(true);
			  OCLFilenameField.setEnabled(true);
			  ProjectName.setEnabled(true);
			  CreateProjectButton.setEnabled(true);
			  ImportXMIFilenameField.setEnabled(false);
			  LoadXMIFileButton.setEnabled(false);
		  }
	  }
	  else { // the filename must be inserted
		  // enable the XMI-load components 
		  ImportXMIFilenameField.setEnabled(true);
		  LoadXMIFileButton.setEnabled(true);
		  FeedBackArea.setText("Firstly, insert a XMI filename");
	  }
 }
	    
  private void LoadOCLFileButtonActionPerformed(java.awt.event.ActionEvent evt)                                         
  {
	  // path of the file that constains OCL 2.0 constraints
	  String path = new String("ocl/"+OCLFilenameField.getText());
	  if (OCLFilenameField.getText().equals("")) FeedBackArea.setText("Firstly, insert a filename.");
	  else {
		  try {
			  // Code for read the file and set the lines into de constraints area
			  BufferedReader br = new BufferedReader(
					  new InputStreamReader(new FileInputStream(path)));
			  ConstraintsArea.setText("");
			  String line = br.readLine();
			  while ( line != null ) {
				  ConstraintsArea.append(line);
				  ConstraintsArea.append("\n");
				  line = br.readLine();
			  }
			  br.close();
			  FeedBackArea.setText("File with OCL constraints '"+path+"' read succesfully");
		  } catch (IOException ex) {
			  FeedBackArea.setText("Error reading file " + path + "! Check system error stream for details.");
			  ex.printStackTrace(System.err);
		  }
	  }
  }

  private void SaveToXMIButtonActionPerformed(java.awt.event.ActionEvent evt)                                         
  {
	  // Code for the button named "SaveToXMIButton"
	  start_time = System.currentTimeMillis();
	  String nomarxiu = SaveToXMIFilenameField.getText();
	  boolean esbuit = nomarxiu.equals("");
	  if (!esbuit) {
		  try {
			  FeedBackArea.setText("Copying data from repository to XMI file "+nomarxiu+" ...");
			  // Disable some GUI components
			  SaveToXMIFilenameField.setEnabled(false);
			  SaveToXMIButton.setEnabled(false);
			  // Save the data into the new file
			  p.saveXMI("models/"+nomarxiu);
			  //ConversorToPoseidon conv = new ConversorToPoseidon();
			  //conv.convert("/Users/lotendil/Desktop/models/"+nomarxiu, "/Users/lotendil/Desktop/models/PSD"+nomarxiu, "GMC");
		  } catch (Exception e) {
			  FeedBackArea.setText("Error in the export process: "+e.getMessage());
			  e.printStackTrace();
			  esbuit = true;
		  }
		  // Enable some GUI components
		  SaveToXMIFilenameField.setEnabled(true);
		  SaveToXMIButton.setEnabled(true);
			
		  stop_time = System.currentTimeMillis();
		  if (!esbuit) {
			  FeedBackArea.append("OK!\n");
			  FeedBackArea.append("Copy time: " + (stop_time - start_time) + " ms\n");
		  }
	  }
	  else {
		  // the filename was empty
		  FeedBackArea.setText("Firstly, insert a name for the new file.");
	  }  
  }

  private void ParseButtonActionPerformed(java.awt.event.ActionEvent evt)                                        
  {
	  // Code for the button named "ParseButton"
	  
	  // Firstly, we get the text to parse
	  boolean rollbackneeded = false;
	  String text = ConstraintsArea.getText();
	  
	  if (text.equals("")) {
		  FeedBackArea.setText("Firstly, write the OCL 2.0 constraints");
	  }
	  else { // Begin Compilation
		  // Create Lexer and Parser 
		  Lexer lexer = new Lexer (new PushbackReader(new StringReader(text),1024));
		  Parser parser = new Parser(lexer);
		  try {
			  // Begin Transaction
			  repository = MDRManager.getDefault().getDefaultRepository();
			  repository.beginTrans(true);
			  
			  // Parse OCL2.0 constraints (syntax)
			  FeedBackArea.setText("Checking syntax.........");
			  Start ast = parser.parse();
			  FeedBackArea.append("OK!\n");
			  start_time = System.currentTimeMillis();
        
			  // Semantics
			  FeedBackArea.append("Checking semantics...");	         
			  // We use a Visitor Pattern
			  ast.apply(new LAttrGMCAstGenerator(p), new Heritage());
        
			  stop_time = System.currentTimeMillis();
			  FeedBackArea.append("OK!\n");
			  FeedBackArea.append("Compilation time: " + (stop_time - start_time) + " ms\n");       
		  } 
		  catch (ParserException pe) {
			  FeedBackArea.setText("Parse error at "+pe.getToken().getText()+" : "+pe.getMessage());
			  rollbackneeded = true;
		  } 
		  catch (Exception e) {
			  FeedBackArea.setText("Incorrect OCL Expression");
			  if (e.getMessage()!=null) {
				  FeedBackArea.append("\nProblem: "+e.getMessage());
				  e.printStackTrace();
			  }
			  else {
				  FeedBackArea.append("\nInternal Error. Report to the GMC developers");
				  e.printStackTrace();
			  }
			  rollbackneeded = true;
		  }
		  finally {
			  // Close the transaction
			  // If rollback is needed, it drops the new changes
			  repository.endTrans(rollbackneeded);
		  }
	  }
  }

  private void EraseOCLAreaButtonActionPerformed(java.awt.event.ActionEvent evt)                                        
  {
	  // Erase the text of the constraints area
	  ConstraintsArea.setText("");
  }
  
  /**
   * @param args the command line arguments
   */
  public static void main(String args[])
  {
    java.awt.EventQueue.invokeLater(new Runnable()
    {
      public void run()
      {
    	  DemoOcl20Parser docl = new DemoOcl20Parser();
    	  
    	  // Process to center the parser window on the screen
    	  Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    	  int X = (screen.width / 2) - (docl.getWidth() / 2); // Center horizontally.
    	  int Y = (screen.height / 2) - (docl.getHeight() / 2); // Center vertically.
    	  docl.setBounds(X, Y, docl.getWidth(), docl.getHeight());
    	  
    	  docl.setVisible(true);
      }
    });
  }
  
  //GEN-BEGIN:variables
  private javax.swing.JTextField OCLFilenameField;
  private javax.swing.JButton LoadOCLFileButton;
  private javax.swing.JButton LoadXMIFileButton;
  private javax.swing.JButton CreateProjectButton;
  private javax.swing.JButton EraseOCLAreaButton;
  private javax.swing.JButton SaveToXMIButton;
  private javax.swing.JTextArea FeedBackArea;
  private javax.swing.JTextField SaveToXMIFilenameField;
  private javax.swing.JTextField ImportXMIFilenameField;
  private javax.swing.JButton ParseButton;
  private javax.swing.JTextArea ConstraintsArea;
  private javax.swing.JTextField ProjectName;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JLabel jLabel6;
  private javax.swing.JLabel jLabel7;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JPanel jPanel3;
  private javax.swing.JPanel jPanel4;
  private javax.swing.JPanel jPanel5;
  private javax.swing.JPanel jPanel6;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JScrollPane jScrollPane2;
  //GEN-END:variables
  
}
